﻿
$(document).ready(function () {
    $("#fr-searchtradecustomer").submit(function () {
        var serializedForm = $(this).serialize();
        var results = $("#cb-tradecustomer");
        App.blockUI($('body'), false);
        $.ajax({
            url: "/trade/searchtradecustomer",
            data: serializedForm,
            type: 'POST',
            success: function (data) {
                results.html('');
                results.append($('<option></option>').html("Please select"));
                $.each(data, function (id, option) {
                    results.append($('<option></option>').val(option.id).html(option.name));
                });
                App.unblockUI($('body'));
                //$(".pn-accountdetail").show();
            }
        });
        return false;
    });

    $("#fr-tradesearchproduct").submit(function () {
        var serializedForm = $(this).serialize();
        var results = $("#lstproductresult");
        App.blockUI($('#productprice'), false);
        $.ajax({
            url: "/trade/TradeSearchProductKybotech",
            data: serializedForm,
            type: 'POST',
            success: function (data) {
                results.html('');
                $.each(data, function (id, option) {
                    results.append($('<option></option>').val(option.id).html(option.name));
                });
                App.unblockUI($('#productprice'));
            }
        });
        return false;
    });

    $("#fr-currentproductalias").submit(function () {
        var serializedForm = $(this).serialize();
        App.blockUI($('.tblproductalias'), false);
        $.ajax({
            url: "/trade/CurrentProductPriceList",
            data: serializedForm,
            success: function (data) {
                $('.tblproductalias').html(data);
                $(".tbl-selected tbody tr").click(function () {
                    $(this).addClass("row-selected");
                    $(".tbl-selected tbody tr").not(this).removeClass('row-selected');
                });
            }
        });
        return false;
    });
    
    $("#frOrderHistory").submit(function () {
        var serializedForm = $(this).serialize();
        App.blockUI($('#FilterResult'), false);
        $.ajax({
            url: "/trade/OrderHistory",
            method:"POST",
            data: serializedForm,
            success: function (data) {
                $('#FilterResult').html(data);
                App.unblockUI($('#FilterResult'), false);
            }, error: function () {
                App.unblockUI($('#FilterResult'), false);
            }
        });
        return false;
    });
    $("#fr-createproductalias").submit(function () {
        var serializedForm = $(this).serialize();
        App.blockUI($('#productprice'), false);
        $.ajax({
            url: "/trade/createproductalias",
            type: 'POST',
            data: serializedForm,
            success: function (data) {
                alert(data);
                App.unblockUI($('#productprice'));
                $("#fr-currentproductalias").submit();
            }
        });
        return false;
    });
        $(".btn-updatemargin").click(function() {
        App.blockUI($('body'), false);
        $.ajax({
            url: "/trade/updatemargin?accnumber=" + $("#cb-tradecustomer").val(),
            success: function () {
                App.unblockUI($('body'));
                alert("Update Completed!");
            }
        });
    });


    $("#fr-uploadtradeorder").submit(function () {
        if ($('#IsSubmitOrder').val() == 1) {
            App.blockUI($('#previeworderresult'), false);
            $.ajax({
                type: "POST",
                url: '/trade/SubmitOrders',
                data: $(this).serialize(),
                success: function (data) {
                    $('#previeworderresult').html(data);
                    $("#submitOrders").attr("disabled", "disabled");
                    App.unblockUI($('#previeworderresult'), false);
                },
                error: function (error) {
                    $('#previeworderresult').html("");
                    App.unblockUI($('#previeworderresult'), false);
                }
            });
        } else {
            if ($('#FileName').val() == "") {
                alert('Please select file to upload');
            } else {
                App.blockUI($('#previeworderresult'), false);
                $.ajax({
                    type: "POST",
                    url: '/trade/previewOrders',
                    data: $(this).serialize(),
                    success: function(data) {
                        $('#previeworderresult').html(data);
                        if ($('#HasOrderErr').val() == 1) {
                            $("#submitOrders").removeAttr("disabled");
                        } else {
                            $("#submitOrders").attr("disabled", "disabled");
                        }
                        App.unblockUI($('#previeworderresult'), false);
                    },
                    error: function(error) {
                        $('#previeworderresult').html("");
                        App.unblockUI($('#previeworderresult'), false);
                    }
                });
            }
        }
        return false;
    });
    $(".btn-updatemargin").click(function() {
        App.blockUI($('body'), false);
        $.ajax({
            url: "/trade/updatemargin?accnumber=" + $("#cb-tradecustomer").val(),
            success: function () {
                App.unblockUI($('body'));
                alert("Update Completed!");
            }
        });
    });

    $("#fr-uploadfile").submit(function () {
        App.blockUI($('#bulkload'), false);
            //grab all form data  
            var formData = new FormData($("#fr-uploadfile")[0]);
            $.ajax({
                url: '/trade/UploadExcelTradeOrders',
                type: 'POST',
                data: formData,
                async: false,
                cache: false,
                contentType: false,
                processData: false,
            success: function (data) {
                if (data != "") {
                    $('#FileName').val(data);
                    $('#uploadSuccess').show();
                } else {
                    $('#FileName').val("");
                    $('#uploadSuccess').hide();
                }
                App.unblockUI($('#bulkload'), false);
            },
            error: function(error) {
                $('#FileName').val("");
                $('#uploadSuccess').hide();
                App.unblockUI($('#bulkload'), false);
            }
        });
        return false;
    });
    $('#frSearchPendingInvoice').submit(function() {
        App.blockUI($('#FilterPendingInvoiceResult'), false);
        $.ajax({
            url: "/trade/SearchPendingInvoiceOrder",
            data: $(this).serialize(),
            cache: false,
            method:"POST",
            success: function (data) {
                $("#FilterPendingInvoiceResult").html(data);
                initTableEvent();
                App.unblockUI($('#FilterPendingInvoiceResult'));
            }, error: function () {
                alert("Error!");
                App.unblockUI($('#FilterPendingInvoiceResult'));
            }
        });
        return false;
    });
    $('#frSearchPendingInvoiceAudit').submit(function () {
        App.blockUI($('#FilterPendingInvoiceAuditResult'), false);
        $.ajax({
            url: "/trade/SearchPendingInvoiceOrderAudit",
            data: $(this).serialize(),
            cache: false,
            method: "POST",
            success: function (data) {
                $("#FilterPendingInvoiceAuditResult").html(data);
                initTableEvent();
                App.unblockUI($('#FilterPendingInvoiceAuditResult'));
            }, error: function () {
                alert("Error!");
                App.unblockUI($('#FilterPendingInvoiceAuditResult'));
            }
        });
        return false;
    });
});
function createnewaccount() {
    $(".pn-accountdetail").show();
}

function tradeproductselected(productid, priceid) {
    $("#hfproductid").val(productid);
    App.blockUI($('#productprice'), false);
    $.ajax({
        url: "/trade/GetTradeProductDetail",
        data: { productid: productid , priceid : priceid},
        cache: false,
        success: function (data) {
            $("#pnTradeproductdetail").html(data);
            App.unblockUI($('#productprice'));
        }, error: function () {
            alert("Error!");
            App.unblockUI($('#productprice'));
        }
    });
}

function tradeproductoptionschange(productoptionid, priceid) {
    App.blockUI($('#pnproductoptioncontrol'), false);
    $.ajax({
        url: "/trade/TradeproductGetSubOptionsDetail",
        data: { productoptionid: productoptionid, priceid : priceid },
        cache: false,
        success: function (data) {
            $("#pnproductoptioncontrol").html(data);
        },error:function() {
            App.unblockUI($('#pnproductoptioncontrol'));
        }
    });
}

function currentproductaliassearch() {
    $("#fr-currentproductalias").submit();
}
function SaveAccountDetail() {
    if ($('#FullCompanyName').val() == "") {
        alert("Please enter Full Company Name!");
        $('#FullCompanyName').focus();
        return;
    }
    if ($('#TradeAccountName').val() == "") {
        alert("Please enter Kybo Short Name!");
        $('#TradeAccountName').focus();
        return;
    }
    $('#fr-accountinfo').submit();
}

function deletecurrentproductprice(id, accNumber) {
    if (id == 0) {
        alert("Please select a row");
    } else {
        if (confirm('Are you sure you wich to delete this product from the trade products list')) {
            App.blockUI($('.tblproductalias'), false);
            $.ajax({                
                url: "/trade/deletecurrentproductprice?id=" + id + "&accNumber=" + accNumber,
                success : function(data) {
                    $('.tblproductalias').html(data);
                    $(".tbl-selected tbody tr").click(function () {
                        $(this).addClass("row-selected");
                        $(".tbl-selected tbody tr").not(this).removeClass('row-selected');
                    });
                }
            });
        }
    }
}

function currentproductpriceselected(priceid, accNumber, productid) {
    $(".btn-deletecurrentproductprice").attr("onclick", "deletecurrentproductprice('" + priceid + "','" + accNumber + "')");
    tradeproductselected(productid, priceid);
}

function previousinvoicesselected(invoiceid) {
    App.blockUI($('#invoicing'), false);
    var accNumber = $("#TradeAccNumber").val();
    $(".btn-generateinvoice").attr("href", "/trade/generateinvoice?invoiceNumber=" + invoiceid + "&accNumber=" + accNumber + "&template=" + $("#cb-typeinvoice").val());
    $.ajax({
        url: "/trade/getinvoiceorderdetail",
        data: { invoiceid: invoiceid},
        cache: false,
        success: function (data) {
            $(".selected-invoices-order").html(data);
            App.unblockUI($('#invoicing'));
        },error: function() {
            alert('receive data error!');
        }
    });
    
}

function cbinvoicechange(invoicevalue) {
    App.blockUI($('body'), false);
    var accNumber = $("#hfaccNumber").val();
    $.ajax({        
        url: "/trade/getdetailcreateinvoice",
        data: { invoicevalue: invoicevalue, accNumber: accNumber },
        success:function(data) {
            App.unblockUI($('body'));
            $(".pncreatenewinvoice").html(data);
        }
    });
}

function reloadpreviousinvoices(accNumber) {
    App.blockUI($('#invoicing'), false);
    $.ajax({
        url: "/trade/getallinvoice",
        data: { accNumber: accNumber },
        success: function(data) {
            $(".previous-invoices").html(data);
            App.unblockUI($('#invoicing'));
            $(".tbl-selected tbody tr").click(function() {
                $(this).addClass("row-selected");
                $(".tbl-selected tbody tr").not(this).removeClass('row-selected');
            });
        },
        error: function() {
            alert('receive data error!');
        }
    });
}

function previewExcelOrders() {
    $("#IsSubmitOrder").val(0);
    $("#fr-uploadtradeorder").submit();
}
function submitExcelOrders() {
    $("#IsSubmitOrder").val(1);
    $("#fr-uploadtradeorder").submit();
}
function FilterByTradeOrderRef(tradeOrderRef) {
    App.blockUI($('#FilterPendingInvoiceResult'), false);
    $.ajax({
        url: "/trade/SearchPendingInvoiceByTradeOrderRef",
        data: {tradeOrderRef:tradeOrderRef},
        cache: false,
        method: "POST",
        success: function (data) {
            $("#FilterPendingInvoiceResult").html(data);
            initTableEvent();
            App.unblockUI($('#FilterPendingInvoiceResult'));
        }, error: function () {
            alert("Error!");
            App.unblockUI($('#FilterPendingInvoiceResult'));
        }
    });
    return false;
}
function FilterByPORef(poRef) {
    App.blockUI($('#FilterPendingInvoiceResult'), false);
    $.ajax({
        url: "/trade/SearchPendingInvoiceByPoRef",
        data: { poRef: poRef },
        cache: false,
        method: "POST",
        success: function (data) {
            $("#FilterPendingInvoiceResult").html(data);
            initTableEvent();
            App.unblockUI($('#FilterPendingInvoiceResult'));
        }, error: function () {
            alert("Error!");
            App.unblockUI($('#FilterPendingInvoiceResult'));
        }
    });
    return false;
}
function initTableEvent() {
    frConfirmPendingOrderSubmit();
    $(".tbl-selected tbody tr").click(function (e) {
        $(this).addClass("row-selected");
        $(".tbl-selected tbody tr").not(this).removeClass('row-selected');
        if (e.target.nodeName!="INPUT") {
            var check = $(this).find("input");
            check.prop('checked', !check.is(':checked'));
        }
        
    });
    var $table = $('.fixhead .table-fix');
    $table.floatThead({
        scrollContainer: function ($table) {
            return $table.closest('.fixhead');
        }
    });
}
function ChangeSelecAll(selected) {
    $("#tbPendingInvoiceOrder tbody tr input").each(function () {
        $(this).prop('checked', selected);
    });
}
function frConfirmPendingOrderSubmit() {
    $('#frConfirmPendingOrder').submit(function () {
        var countSelected = $("#tbPendingInvoiceOrder tbody tr input:checked").size();
        if (countSelected==0) {
            alert("Please select an order");
            return false;
        }
        if (confirm(("Are you sure you want to " + ($("#isCancel").val() == 1 ? "cancel" : "confirm") + " " + countSelected + " order?")))
        {
            App.blockUI($('#pendinginvoiceorders'), false);
            $.ajax({
                url: "/trade/ConfirmPendingOrders",
                data: $(this).serialize(),
                cache: false,
                method: "POST",
                success: function(data) {
                    if (data != "") {
                        alert(data);
                        FilterByPORef("");
                    } else alert("Success!");
                    App.unblockUI($('#pendinginvoiceorders'));
                },
                error: function() {
                    alert("Error!");
                    App.unblockUI($('#pendinginvoiceorders'));
                }
            });
        }
        return false;
    });
}
function FilterAuditByTradeOrderRef(tradeOrderRef) {
    App.blockUI($('#FilterPendingInvoiceAuditResult'), false);
    $.ajax({
        url: "/trade/SearchPendingInvoiceAuditByTradeOrderRef",
        data: { tradeOrderRef: tradeOrderRef },
        cache: false,
        method: "POST",
        success: function (data) {
            $("#FilterPendingInvoiceAuditResult").html(data);
            initTableEvent();
            App.unblockUI($('#FilterPendingInvoiceAuditResult'));
        }, error: function () {
            alert("Error!");
            App.unblockUI($('#FilterPendingInvoiceAuditResult'));
        }
    });
    return false;
}
function FilterAuditByPORef(poRef) {
    App.blockUI($('#FilterPendingInvoiceAuditResult'), false);
    $.ajax({
        url: "/trade/SearchPendingInvoiceAuditByPoRef",
        data: { poRef: poRef },
        cache: false,
        method: "POST",
        success: function (data) {
            $("#FilterPendingInvoiceAuditResult").html(data);
            initTableEvent();
            App.unblockUI($('#FilterPendingInvoiceAuditResult'));
        }, error: function () {
            alert("Error!");
            App.unblockUI($('#FilterPendingInvoiceAuditResult'));
        }
    });
    return false;
}
function cbinvoicechange(invoicevalue) {
    App.blockUI($('body'), false);
    var accNumber = $("#hfaccNumber").val();
    $.ajax({        
        url: "/trade/getdetailcreateinvoice",
        data: { invoicevalue: invoicevalue, accNumber: accNumber },
        success:function(data) {
            App.unblockUI($('body'));
            $(".pncreatenewinvoice").html(data);
        }
    });
}

function reloadpreviousinvoices(accNumber) {
    App.blockUI($('#invoicing'), false);
    $.ajax({
        url: "/trade/getallinvoice",
        data: { accNumber: accNumber },
        success: function(data) {
            $(".previous-invoices").html(data);
            App.unblockUI($('#invoicing'));
            $(".tbl-selected tbody tr").click(function() {
                $(this).addClass("row-selected");
                $(".tbl-selected tbody tr").not(this).removeClass('row-selected');
            });
        },
        error: function() {
            alert('receive data error!');
        }
    });
}
